package edu.hwadee.BEIS.controller;

import javax.net.ssl.HttpsURLConnection;
import javax.servlet.http.HttpSession;

import edu.hwadee.BEIS.constants.CustomException;
import edu.hwadee.BEIS.dto.SchoolConditionDTO;
import edu.hwadee.BEIS.entity.*;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.bind.annotation.*;

import edu.hwadee.BEIS.util.ResultUtil;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.net.URLEncoder;

/**
 * 学校用户Controller
 *
 * @author kingsong
 */
@RestController
@EnableAsync
@RequestMapping("/school")
public class SchoolUserController extends BaseController {

    /**
     * 获得学校基本情况
     *
     * @param
     * @return
     * @throws Exception
     */
    @GetMapping(value = "/analysisdata/schoolbasicinfor", produces = "application/json;charset=utf-8")
    public String getSchoolBasicInfor(HttpSession session) throws Exception {
        JsonResult res = ResultUtil.success();

        String schoolUser_id = (String) session.getAttribute("user_id");
        res.setData(schoolUserService.getSchoolBasicInfor(schoolUser_id));

        return res.toString();
    }

    /**
     * 获得学校学生基本情况
     *
     * @param session
     * @return
     * @throws Exception
     */
    @GetMapping(value = "/analysisdata/studentbasicsituation", produces = "application/json;charset=utf-8")
    public String getStudentBasicSituation(HttpSession session) throws Exception {
        JsonResult res = ResultUtil.success();
        String schoolUser_id = (String) session.getAttribute("user_id");
        res.setData(schoolUserService.getStudentBasicSituationForOneYear(schoolUser_id));
        return res.toString();
    }

    /**
     * 图表分析：获得学校办学情况信息
     *
     * @param session
     * @return
     */
    @GetMapping(value = "/analysisdata/schoolcondition", produces = "application/json;charset=utf-8")
    public String getSchoolCondition(HttpSession session) throws Exception {
        JsonResult res = ResultUtil.success();
        res.setData(schoolUserService.getSchoolCondition((String) session.getAttribute("user_id")));
        return res.toString();
    }

    /**
     * 图表分析：获得全校实训基地情况信息
     *
     * @param session
     * @return
     */
    @GetMapping(value = "/analysisdata/wholeschooltrainingbase", produces = "application/json;charset=utf-8")
    public String getWholeSchoolTrainingBase(HttpSession session) throws Exception {
        JsonResult res = ResultUtil.success();
        res.setData(schoolUserService.getWholeSchoolTrainingBaseForOneYear((String) session.getAttribute("user_id")));
        return res.toString();
    }

    /**
     * 图表分析：获得公用实训基地情况信息
     *
     * @param session
     * @return
     * @throws Exception
     */
    @GetMapping(value = "/analysisdata/publictrainingbase", produces = "application/json;charset=utf-8")
    public String getPublicTrainingBase(HttpSession session) throws Exception {
        JsonResult res = ResultUtil.success();
        res.setData(schoolUserService.getNewestPublicTrainingBase((String) session.getAttribute("user_id")));
        return res.toString();
    }


    /*
     * 未完成：
     * 判断是否开放填报
     * 填报信息列表（分页）
     *
     *
     */

    /**
     * 添加教师基本情况
     *
     * @param tbs 教师基本情况对象
     * @throws Exception
     */
    @PutMapping(value = "/Teacher/BasicSituation", produces = "application/json;charset=utf-8")
    public String addTeacherBasicSituation(TeacherBasicSituation tbs, HttpSession session) throws Exception {
        String user_id = (String)session.getAttribute("user_id");
        schoolUserService.addTeacherBasicSituation(tbs, user_id);
        JsonResult js = ResultUtil.success();
        return js.toString();
    }

    /**
     * 拉取教师基本情况（分页）
     *
     * @param page page(n)、size
     * @return Json
     * @throws Exception
     */
    @PostMapping(value = "/Teacher/BasicSituation", produces = "application/json;charset=utf-8")
    public String getTeacherBasicSituation(Pageable page, HttpSession session) throws Exception {
        String user_id = (String) session.getAttribute("user_id");
        Page<TeacherBasicSituation> result = schoolUserService.getTeacherBasicSituations(page, user_id);
        return ResultUtil.success(result).toString();
    }

    @ResponseBody
    @PutMapping(value = "/Teacher/MajorSituation", produces = "application/json;charset=utf-8")
    public void addMajorTeacherSituation() throws Exception {

    }

    @PostMapping(value = "/Teacher/MajorSituation", produces = "application/json;charset=utf-8")
    public String getMajorTeacherSituation(Pageable page) throws Exception {
        //Page<TeacherBasicSituation> result = schoolUserService.getTeacherBasicSituations(page);
        JsonResult js = ResultUtil.success();
        //js.setData(result);
        return js.toString();
    }

    @ResponseBody
    @PutMapping(value = "/Teacher/PublicCourseSituation", produces = "application/json;charset=utf-8")
    public void addPublicCourseTeacherSituation() throws Exception {

    }

    @GetMapping(value = "/Teacher/PublicCourseSituation", produces = "application/json;charset=utf-8")
    public String getPublicCourseTeacherSituation() throws Exception {
        Page<TeacherBasicSituation> result = null;
        return null;
    }

    /**
     * 添加专业年级课程构建
     * @param file
     * @param session
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/majorgradecourseconstruction", produces = "application/json;charset=utf-8")
    public String majorGradeCourseConstruction(MultipartFile file, HttpSession session) throws Exception {
        JsonResult res = ResultUtil.success();
        SchoolBasicInfor schoolBasicInfor = schoolUserService.getSchoolBasicInfor((String) session.getAttribute("user_id"));
        schoolUserService.addMajorGradeCourseConstruction(file,schoolBasicInfor);

        return res.toString();
    }

    /**
     * 添加参与职教集团情况
     * @param file
     * @param session
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/vocationaleducationalgroup", produces = "application/json;charset=utf-8")
    public String vocationalEducationalGroup(MultipartFile file, HttpSession session) throws Exception {
        JsonResult res = ResultUtil.success();
        SchoolBasicInfor schoolBasicInfor = schoolUserService.getSchoolBasicInfor((String) session.getAttribute("user_id"));
        schoolUserService.addVocationalEducationalGroup(file,schoolBasicInfor);

        return res.toString();
    }

    /**
     * 添加国际合作信息
     * @param file
     * @param session
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/globalcooperation", produces = "application/json;charset=utf-8")
    public String globalCooperation(MultipartFile file, HttpSession session) throws Exception {
        JsonResult res = ResultUtil.success();
        SchoolBasicInfor schoolBasicInfor = schoolUserService.getSchoolBasicInfor((String) session.getAttribute("user_id"));
        schoolUserService.addGlobalCooperation(file,schoolBasicInfor);

        return res.toString();
    }

    /**
     * 添加校企合作信息
     * @param file
     * @param session
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/schoolcompanycooperation", produces = "application/json;charset=utf-8")
    public String schoolCompanyCooperation(MultipartFile file, HttpSession session) throws Exception {
        JsonResult res = ResultUtil.success();
        SchoolBasicInfor schoolBasicInfor = schoolUserService.getSchoolBasicInfor((String) session.getAttribute("user_id"));
        schoolUserService.addSchoolCompanyCooperation(file,schoolBasicInfor);

        return res.toString();
    }
    /**
     * 添加学校基本信息
     * @param schoolBasicInfor
     * @param session
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/schoolbasicinfor", produces = "application/json;charset=utf-8")
    public String schoolbasicinfor_1(SchoolBasicInfor schoolBasicInfor, HttpSession session) throws Exception {
        JsonResult res = ResultUtil.success();
        schoolUserService.addSchoolBasicInfor(schoolBasicInfor);
        return res.toString();
    }

    @PutMapping(value = "/basicinfor", produces = "application/json;charset=utf-8")
    public String updateSchoolBasicInfor(SchoolBasicInfor sbi, HttpSession session) throws CustomException{
        String user_id = (String)session.getAttribute("user_id");
        schoolUserService.updateSchoolBasicInfor(sbi, user_id);
        JsonResult res = ResultUtil.success();
        return res.toString();
    }

    /**
     * 添加办学条件
     * @param schoolConditionDTO
     * @param session
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/schoolcondition", produces = "application/json;charset=utf-8")
    public String schoolcondition_1(SchoolConditionDTO schoolConditionDTO, HttpSession session) throws Exception {
        JsonResult res = ResultUtil.success();
//        String userId = (String) session.getAttribute("user_id");
        SchoolBasicInfor schoolBasicInfor = schoolUserService.findSchoolBasicInfor("1");
        schoolUserService.addSchoolCondition(schoolConditionDTO,schoolBasicInfor);

        return res.toString();
    }

    /**
     * 添加项目申报信息
     * @param multipartFile
     * @param pd
     * @param session
     * @return
     * @throws Exception
     */
    @PutMapping(value = "/ProjectDeclare", produces = "application/json;charset=utf-8")
    public String addProjectDeclare(MultipartFile multipartFile, ProjectDeclare pd, HttpSession session) throws Exception {
        String u_id = (String)session.getAttribute("user_id");
        schoolUserService.addProjectDeclare(pd, u_id, multipartFile);
        JsonResult js = ResultUtil.success();
        return js.toString();
    }
}

